package com.gec.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gec.entity.Buycar;
import com.gec.entity.Member;
import com.gec.service.BuycarService;
import com.gec.service.impl.BuycarServiceImpl;
import com.sun.javafx.collections.MappingChange.Map;

@WebServlet("/addcar")
public class BuyCarAddServlet extends HttpServlet{

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Member me = (Member) req.getSession().getAttribute("member");
		resp.setContentType("text/html;charset=utf-8");
		int gid = Integer.parseInt(req.getParameter("goodid"));
		int sid = Integer.parseInt(req.getParameter("shopid"));
		BuycarService bcservlet = new BuycarServiceImpl();
		PrintWriter out = resp.getWriter();
		if(me != null) {
			List<Buycar> lbc = bcservlet.showAll(me.getId(),sid);
			HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
			for (Buycar c : lbc) {
				map.put(c.getGood_id(), c.getQty());
			}
			req.setAttribute("map", map);
//			Buycar bc1 = new Buycar();
//			bc1.setMem_id(me.getId());
//			bc1.setGood_id(gid);			
//			bcservlet.addBuyCar(me.getId(),gid);			
			int allqty = bcservlet.findtotal(me.getId(),sid);
			req.setAttribute("allqty", allqty);
			out.print(allqty>0?allqty:"-1");
		}else {
			out.print(me!=null?"1":"-1");
		}
		out.close();		
	}
	
}
